home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / misc / aterminfo.lha / dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  3.3 KB  |  169 lines

  1.  
  2. /* This work is copyrighted. See COPYRIGHT.OLD & COPYRIGHT.NEW for   *
  3. *  details. If they are missing then this copy is in violation of    *
  4. *  the copyright conditions.                                        */
  5.  
  6. /*
  7.  *    dump.c - dump the contents of a compiled terminfo file in a
  8.  *         human-readable format.
  9.  *
  10.  */
  11.  
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "compiler.h"
  15. #include "term.h"
  16.  
  17. extern int read_entry(char *, TERMINAL*);
  18.  
  19. static void expand(unsigned char *);
  20. static void usage(void);
  21.  
  22. void main(argc, argv)
  23. int    argc;
  24. char    *argv[];
  25. {
  26. int        i;
  27. int        cur_column;
  28. char        buffer[1024];
  29. TERMINAL    current_term;
  30. char        *terminal;
  31. char        *terminfo;
  32.  
  33.     cur_term = ¤t_term;
  34.  
  35.     if((terminal = getenv("TERM")) == NULL)
  36.     {
  37.         fprintf(stderr,"untic: environment variable TERM not set\n");
  38.         exit(1);
  39.     }
  40.  
  41.     if((terminfo = getenv("TERMINFO")) == NULL)
  42.     {
  43.         terminfo = SRCDIR;
  44.     }
  45.  
  46.     switch(argc) {
  47.     case 1:
  48.         /* no parameters */
  49. #ifdef AMIGA
  50.         sprintf(buffer, "%s%c/%s", terminfo, *terminal, terminal);
  51. #else
  52.         sprintf(buffer, "%s/%c/%s", terminfo, *terminal, terminal);
  53. #endif
  54.         break;
  55.     case 2:
  56.         /* parameter is terminal name */
  57.         if(argv[1][0] == '-' && (argv[1][1] == '?' || argv[1][1] == 'h'))
  58.             usage();
  59.         else
  60. #ifdef AMIGA
  61.             sprintf(buffer, "%s%c/%s", terminfo, *argv[1], argv[1]);
  62. #else
  63.             sprintf(buffer, "%s/%c/%s", terminfo, *argv[1], argv[1]);
  64. #endif
  65.         break;
  66.     case 3:
  67.         /* parameter is "-f filename" */
  68.         if(argv[1][0] == '-' && argv[1][1] == 'f')
  69.             sprintf(buffer, "%s", &argv[1][3]);
  70.         else
  71.             usage();
  72.         break;
  73.     default:
  74.         usage();
  75.     }
  76.  
  77.     if (read_entry(buffer, cur_term) < 0) {
  78.         fprintf(stderr, "file %s may not be a terminfo entry\n", buffer);
  79.         exit(1);
  80.     }
  81.  
  82.     printf("%s,\n\t", cur_term->term_names);
  83.     cur_column = 9;
  84.  
  85.     for (i=0; i < BOOLCOUNT; i++)
  86.     {
  87.         if (cur_term->Booleans[i] == TRUE)
  88.         {
  89.             if (cur_column > 9 &&  cur_column + strlen(boolnames[i]) + 2 > 79)
  90.             {
  91.                 printf("\n\t");
  92.                 cur_column = 9;
  93.             }
  94.             printf("%s, ", boolnames[i]);
  95.             cur_column += strlen(boolnames[i]) + 2;
  96.         }
  97.     }
  98.  
  99.     for (i=0; i < NUMCOUNT; i++)
  100.     {
  101.         if (cur_term->Numbers[i] != -1)
  102.         {
  103.             if (cur_column > 9 &&  cur_column + strlen(numnames[i]) + 5 > 79)
  104.             {
  105.                 printf("\n\t");
  106.                 cur_column = 9;
  107.              }
  108.             printf("%s#%d, ", numnames[i], cur_term->Numbers[i]);
  109.             cur_column += strlen(numnames[i]) + 5;
  110.         }
  111.     }
  112.  
  113.     for (i=0; i < STRCOUNT; i++)
  114.     {
  115.         if (cur_term->Strings[i])
  116.         {
  117.             sprintf(buffer, "%s=%s, ", strnames[i], cur_term->Strings[i]);
  118.             expand(buffer);
  119.              if (cur_column > 9  &&  cur_column + strlen(buffer) > 79)
  120.             {
  121.                 printf("\n\t");
  122.                 cur_column = 9;
  123.              }
  124.             printf("%s", buffer);
  125.             cur_column += strlen(buffer);
  126.         }
  127.     }
  128.     putchar('\n');
  129. }
  130.  
  131.  
  132. void expand(unsigned char *str)
  133. {
  134. char    buffer[1024];
  135. int    bufp;
  136. unsigned char    *ptr;
  137.  
  138.     bufp = 0;
  139.     ptr = str;
  140.     while (*str) {
  141.         if (*str < ' ') {
  142.         buffer[bufp++] = '^';
  143.         buffer[bufp++] = *str + '@';
  144.         }
  145.         else if (*str < '\177')
  146.         buffer[bufp++] = *str;
  147.         else {
  148.         sprintf(&buffer[bufp], "\\%03o", *str);
  149.         bufp += 4;
  150.         }
  151.  
  152.         str++;
  153.     }
  154.  
  155.     buffer[bufp] = '\0';
  156.     strcpy(ptr, buffer);
  157. }
  158.  
  159. void
  160. usage()
  161. {
  162.     fprintf(stderr,"\nusage: untic [term] [-f file] - decompile terminfo database entry\n");
  163.     fprintf(stderr,"  term       - terminal name to be decompiled\n");
  164.     fprintf(stderr,"  -f file    - filename to be used to decompile\n");
  165.     fprintf(stderr,"  no options - terminal name and terminfo path got from TERM/TERMINFO\n\n");
  166.     exit(1);
  167. }
  168.  
  169.